package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.MonitorValue;
import com.banksoft.cps.bean.OffsetOrder;
import com.banksoft.cps.bean.SaleOrder;
import com.banksoft.cps.bean.query.OffsetOrderQuery;
import com.banksoft.cps.bean.warpper.OffsetOrderWarpper;
import com.banksoft.cps.dao.*;
import com.banksoft.cps.service.OffsetOrderService;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by rongzhao.zhu on 2018/2/24.
 */
@Service
public class OffsetOrderServiceImpl implements OffsetOrderService {
    @Autowired
    OffsetOrderDao offsetOrderDao;
    @Autowired
    SaleOrderDao saleOrderDao;

    @Autowired
    MonitorValueDao monitorValueDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    BuyCompanyDao buyCompanyDao;

    @Override
    public List<OffsetOrder> list(OffsetOrderQuery query) {
        return offsetOrderDao.query(query);
    }

    @Override
    public List<OffsetOrderWarpper> listWarpper(OffsetOrderQuery query) {
        List<OffsetOrder> offsetOrders = offsetOrderDao.query(query);
        if(!offsetOrders.isEmpty()){
            Map<String,Company> companyMap = new HashMap<>();
            List<String> companyIdList  = new ArrayList<>();
            List<String> saleOrderIdList  = new ArrayList<>();
            offsetOrders.forEach(e->{
                saleOrderIdList.add(e.getSaleOrderId());
                if(StringUtils.isNotBlank(e.getCompanyId())&&!companyIdList.contains(e.getCompanyId())){
                    companyIdList.add(e.getCompanyId());
                }
            });
            List<SaleOrder> saleOrders = saleOrderDao.listIdList(saleOrderIdList);
            List<MonitorValue>  monitorValues = monitorValueDao.listBySaleOrderIdList(saleOrderIdList);
            List<Company> companyList = companyDao.listByIdList(companyIdList);
            if(companyList !=null && companyList.size()>0){
                companyList.forEach(e->{
                    companyMap.put(e.getId(),e);
                });
            }
            Map<String,SaleOrder> saleOrderMap = saleOrders.stream().collect(Collectors.toMap(e->e.getId(), e->e));
            Map<String,MonitorValue> monitorMap = monitorValues.stream().collect(Collectors.toMap(e->e.getSaleOrderId(),e->e));
            return offsetOrders.stream().map(o->{
                OffsetOrderWarpper w =  new OffsetOrderWarpper();
                BeanUtils.copyProperties(o,w);
                SaleOrder s= saleOrderMap.get(o.getSaleOrderId());
                MonitorValue m = monitorMap.get(o.getSaleOrderId());
                Company c = companyMap.get(o.getCompanyId());
                w.setMonitorValue(m);
                w.setSaleOrder(s);
                w.setCompany(c);
                return w;
            }).collect(Collectors.toList());

        }


        return null;
    }

    @Override
    public long count(OffsetOrderQuery query) {
        return offsetOrderDao.count(query);
    }

    @Override
    public void save(OffsetOrder bean) {
        SaleOrder saleOrder = saleOrderDao.findById(bean.getSaleOrderId());
        bean.setCompanyId(saleOrder.getCompanyId());
        bean.setProductId(saleOrder.getProductId());
        if(StringUtils.isNotBlank(bean.getId())){
            offsetOrderDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            offsetOrderDao.insert(bean);
        }
    }

    @Override
    public OffsetOrder findBySaleOrderId(String saleOrderId) {
        OffsetOrderQuery query = new OffsetOrderQuery();
        query.setSaleOrderId(saleOrderId);
        List<OffsetOrder> list = offsetOrderDao.query(query);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
}
